﻿using MiniExcelLibs;
using SqlSugar;
using Synergy.dbmodel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Synergy
{
    public partial class Form人员工序设置 : Form
    {
        List<string> 选择工序 = new List<string>();
        public Form人员工序设置()
        {
            InitializeComponent();
        }

        private void button选工序_Click(object sender, EventArgs e)
        {
            Form工序列表 form = new Form工序列表();
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                选择工序 = form.选择工序;
                this.textBox工序.Text = string.Join(";", 选择工序);
            }
            else
            {
                this.textBox工序.Text = "";
            }
        }

        private void button确定_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox工号.Text) == true)
            {
                MessageBox.Show("请输入工号");
            }
            else
            {
                Z_UA_User z_UA_User = SqlSugarHelp.scope.Queryable<Z_UA_User>().Where(it => it.cUser_Id == textBox工号.Text.Trim()).First();
                if (z_UA_User != null)
                {
                    if (选择工序 != null && 选择工序.Count > 0)
                    {
                        List<Z_人员工序> z_s = new List<Z_人员工序>();
                        for (int i = 0; i < 选择工序.Count; i++)
                        {
                            Z_人员工序 z = new Z_人员工序();
                            z.id = Guid.NewGuid();
                            z.工序名称 = 选择工序[i];
                            z.cUser_Id = z_UA_User.cUser_Id;
                            z.cUser_Name = z_UA_User.cUser_Name;
                            z_s.Add(z);
                        }

                        //int r = SqlSugarHelp.scope.Insertable(z_s).ExecuteCommand();
                       var a = SqlSugarHelp.scope.Storageable(z_s)
                            .WhereColumns(it => new { it.cUser_Id, it.工序名称 }).ToStorage();
                       //var  r = a.AsInsertable.ExecuteCommand();
                        var 插入 = a.InsertList.Select(z => z.Item).ToList();
                        var 已有 = a.UpdateList.Select(z => z.Item).ToList();
                        if (插入 != null && 插入.Count > 0)
                        {
                            a.AsInsertable.ExecuteCommand();
                            MessageBox.Show("设置成功");
                            刷新();
                        }
                        if (已有 != null && 已有.Count > 0)
                        {
                            MessageBox.Show($"工序{string.Join(";", 已有.Select(it =>it.工序名称).ToArray())}已经分配过了");
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择工序");
                    }
                }
                else
                {
                    MessageBox.Show("工号不存在");
                }
            }
        }

        private void textBox工号_TextChanged(object sender, EventArgs e)
        {
            Z_UA_User z_UA_User = SqlSugarHelp.scope.Queryable<Z_UA_User>().Where(it => it.cUser_Id == textBox工号.Text.Trim()).First();
            this.textBox姓名.Text = z_UA_User?.cUser_Name;
        }

        private void 刷新()
        { 
        List<Z_人员工序> z_s = SqlSugarHelp.scope.Queryable<Z_人员工序>().OrderBy(it => it.cUser_Id).ToList();
            this.dataGridView1.DataSource = z_s;
            this.dataGridView1.Refresh();
        }

        private void Form人员工序设置_Load(object sender, EventArgs e)
        {
            刷新();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //如果点击的是删除按钮
            if (dataGridView1.Columns[e.ColumnIndex].Name == "删除")
            {
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string id = this.dataGridView1.Rows[e.RowIndex].Cells["id"].Value.ToString();
                    int r = SqlSugarHelp.scope.Deleteable<Z_人员工序>().Where(it => it.id == new Guid(id)).ExecuteCommand();
                    if (r > 0)
                    {
                        MessageBox.Show("删除成功");
                        刷新();
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
        }

        private void button导出模板_Click(object sender, EventArgs e)
        {
            List<Z_人员工序> zs = new List<Z_人员工序>();
            Z_人员工序 z = new Z_人员工序();
            z.工序名称 = "";
            z.cUser_Id = "";
            z.cUser_Name = "";
            zs.Add(z);
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            saveFileDialog.FileName = $"人员工序{DateTime.Now.ToString("yyMMdd")}";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                MiniExcel.SaveAs(saveFileDialog.FileName, zs, overwriteFile: true);
                MessageBox.Show("导出成功");
            }
        }

        private void button导入_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "XLS,XLSX|*.xls;*.xlsx";
            if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return ;
            }
            string filepath = openFileDialog1.FileName;
            var dataImport = MiniExcel.Query<Z_人员工序>(filepath).ToList();

        }
    }
}
